package com.yyfax.ph.personal.bindapply;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.aspsine.irecyclerview.IViewHolder;
import com.yyfax.common.recyclerview.BaseRecycleAdapter;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;

import java.util.Collection;
import java.util.List;

/**
 * 绑定申请子账户的adapter
 * Created by ZhangXian on 2017/11/8.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */

public class BindApplyAdapter extends BaseRecycleAdapter<BindApplyModel.ApplyListBean> {


	private int ITEM_STATUS_PROCESS = 0;
	private int ITEM_STATUS_OTHER = 1;
	private int ITEM_STATUS_TIPS = 2;
	private int status = ITEM_STATUS_PROCESS;
	private int mTipsPosition = 0;

	public interface BindApplyItemClickListener {
		void onDetailItemClick(String subUserId, String phone, int type);
	}

	BindApplyItemClickListener mListener;

	public void setBindApplyItemClickListener(BindApplyItemClickListener listener) {
		mListener = listener;
	}

	@Override
	public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bind_apply_item, null);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(IViewHolder viewHolder, int position) {
		ViewHolder holder = (ViewHolder) viewHolder;
		if (status == ITEM_STATUS_TIPS) {
			holder.mNormalItemLy.setVisibility(View.GONE);
			holder.mTipsView.setVisibility(View.VISIBLE);
			return;
		}
		if (status == ITEM_STATUS_OTHER) {
			position--;
		}
		holder.mNormalItemLy.setVisibility(View.VISIBLE);
		holder.mTipsView.setVisibility(View.GONE);
		final BindApplyModel.ApplyListBean bean = getDataSet().get(position);
		holder.phoneTv.setText(bean.phone);
		holder.applyDateTv.setText(bean.applyTime);
		if (BindApplyModel.STATUS_PROCESS.equals(bean.status)) {//申请中
			holder.phoneTv.setTextColor(YJPH.getInstance().color(R.color.color_262932));
			holder.applyDateTv.setTextColor(YJPH.getInstance().color(R.color.color_262932));
			holder.statusTv.setVisibility(View.GONE);
			holder.handleLl.setVisibility(View.VISIBLE);

			holder.applyTv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mListener.onDetailItemClick(bean.id, bean.phone, 0);//接受按钮
				}
			});
			holder.refuseTv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mListener.onDetailItemClick(bean.id, bean.phone, 1);//拒绝按钮
				}
			});
		} else {//已接受，已拒绝，已失效
			holder.phoneTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
			holder.applyDateTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
			holder.statusTv.setVisibility(View.VISIBLE);
			holder.handleLl.setVisibility(View.GONE);
			holder.statusTv.setText(bean.statusName);
		}

	}

	@Override
	public void addData(Collection<BindApplyModel.ApplyListBean> data) {
		super.addData(data);
		BindApplyModel.ApplyListBean bean;
		List<BindApplyModel.ApplyListBean> dataSet = getDataSet();
		for (int i = 0; i < dataSet.size(); i++) {
			bean = dataSet.get(i);
			if (!BindApplyModel.STATUS_PROCESS.equals(bean.status)) {
				mTipsPosition = i;
				break;
			}
		}
		if(mTipsPosition == 0){
			status = ITEM_STATUS_PROCESS;
		}
	}

	@Override
	public int getItemCount() {
		if(getCount() == 0){
			return 0;
		}else{
			return mTipsPosition == 0 ? getCount(): getCount() + 1;
		}
	}

	@Override
	public int getItemViewType(int position) {
		if(mTipsPosition == 0){
			return super.getItemViewType(position);
		}
		if (mTipsPosition == position) {
			status = ITEM_STATUS_TIPS;
		} else if (mTipsPosition < position) {
			status = ITEM_STATUS_OTHER;
		} else {
			status = ITEM_STATUS_PROCESS;
		}
		return super.getItemViewType(position);
	}

	class ViewHolder extends IViewHolder {
		@BindView(R.id.phone_tv)
		TextView phoneTv;
		@BindView(R.id.status_tv)
		TextView statusTv;
		@BindView(R.id.apply_date_tv)
		TextView applyDateTv;
		@BindView(R.id.handle_ll)//处理布局
				LinearLayout handleLl;
		@BindView(R.id.apply_btn)//接受
				Button applyTv;
		@BindView(R.id.refuse_btn)//拒绝
				Button refuseTv;
		@BindView(R.id.normal_item_ly)
		RelativeLayout mNormalItemLy;
		@BindView(R.id.tips_view)
		View mTipsView;

		public ViewHolder(View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}
}